package com.uusee.crawler.job;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.uusee.shipshape.sp.model.Ugc;
import com.uusee.crawler.fetcher.FetchHTTP;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.pageprocessor.video.PlayPageProcessor;
import com.uusee.crawler.pageprocessor.video.UserVideoPageProcessor;

public class YoukuUserVideoCrawlJob {
	public static void main(String[] args) {
		try {
			YoukuUserVideoCrawlJob job = new YoukuUserVideoCrawlJob();
			CrawlURI crawURI = new CrawlURI();
			crawURI.setSourceSite("www.youku.com");
			crawURI.setCrawlUrl("http://u.youku.com/user_video/id_UMTE0NDQ4OTI=_order_1_type_2_page_2.html");
			crawURI.setModel(new ArrayList<Ugc>());
			job.crawl(crawURI);
			System.out.print("");
		} catch (Exception e) {
		}
	}

	public void crawl(CrawlURI crawURI) {
		try {
			FetchHTTP fetch = new FetchHTTP();
			fetch.process(crawURI);

			UserVideoPageProcessor userVideoPageProcessor = new UserVideoPageProcessor();
			InputStream in = ClassLoader.getSystemResourceAsStream("regex/" + crawURI.getSourceSite()
					+ "/userVideoPage.properties");
			Properties processPageRegex = new Properties();
			processPageRegex.load(in);
			userVideoPageProcessor.setPageProcessRegex(processPageRegex);
			userVideoPageProcessor.process(crawURI);

			List<Ugc> videoList = (List<Ugc>) crawURI.getModel();
			for (Ugc video : videoList) {
				CrawlURI playCrawURI = new CrawlURI();
				playCrawURI.setSourceSite("www.youku.com");
				playCrawURI.setCrawlUrl(video.getPlayUrl());
				playCrawURI.setModel(video);

				FetchHTTP playFetch = new FetchHTTP();
				playFetch.process(playCrawURI);

				PlayPageProcessor playPageProcessor = new PlayPageProcessor();
				InputStream in2 = ClassLoader.getSystemResourceAsStream("regex/" + crawURI.getSourceSite()
						+ "/playPage.properties");
				Properties videosRegex = new Properties();
				videosRegex.load(in2);
				playPageProcessor.setPageProcessRegex(videosRegex);
				playPageProcessor.process(playCrawURI);
				System.out.println(video.getVid());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
